import { getUserInfo, login, register, updateCoin } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = {
  token: getToken(),
  id: '',
  nickname: '',
  avatar: '',
  dynamic: '',
  sex: '太监',
  coin: 500,
  vipLevel: 0,
  funNum: 9,
  followNum: 10,
  exp: 500,
  barNum: 5
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_DYNAMIC: (state, dynamic) => {
    state.dynamic = dynamic
  },
  SET_SEX: (state, sex) => {
    state.sex = sex
  },
  SET_COIN: (state, coin) => {
    state.coin = coin
  },
  SET_VIPLEVEL: (state, vipLevel) => {
    state.vipLevel = vipLevel
  },
  SET_FUNNUM: (state, funNum) => {
    state.funNum = funNum
  },
  SET_FOLLOWNUM: (state, follow) => {
    state.followNum = follow
  },
  SET_EXP: (state, exp) => {
    state.exp = exp
  },
  SET_BARNUM: (state, barNum) => {
    state.barNum = barNum
  }
}

const actions = {
  // 用户登录
  login ({ commit }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      // 调用登录接口
      login({
        username: username.trim(),
        password: password
      }).then(respond => {
        // console.log('store/user/login接口返回数据:', respond)
        // store存储用户信息
        commit('SET_TOKEN', respond.data.token)
        commit('SET_ID', respond.data.id)
        commit('SET_NICKNAME', respond.data.nickname)
        commit('SET_AVATAR', respond.data.avatar)
        commit('SET_SEX', respond.data.sex)
        commit('SET_COIN', respond.data.coin)
        commit('SET_VIPLEVEL', respond.data.vipLevel)
        commit('SET_FUNNUM', respond.data.funNum)
        commit('SET_FOLLOWNUM', respond.data.followNum)
        commit('SET_BARNUM', respond.data.barNum)
        commit('SET_EXP', respond.data.exp)
        // Cookie缓存token
        setToken(respond.data.token, respond.data.id)
        resolve()
      }).catch(error => {
        reject(new Error(error))
      })
    })
  },
  // 退出登录
  logout ({ commit }) {
    return new Promise((resolve) => {
      // 调用退出登录接口
      // logout({ token: state.token }).then(respond => {
      //   commit('SET_TOKEN', '')
      //   removeToken()
      //   resolve()
      // }).catch(error => {
      //   reject(new Error(error))
      // })
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  },
  // 注册账号
  register ({ commit }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      // 调用登录接口
      register({
        username: username.trim(),
        password: password
      }).then(respond => {
        console.log('store/register接口返回数据:', respond)
        resolve()
      }).catch(error => {
        reject(new Error(error))
      })
    })
  },
  // 初始化用户信息
  initUserInfo ({ commit }) {
    return new Promise((resolve, reject) => {
      // 调用获取用户信息接口
      getUserInfo(getToken()).then(respond => {
        // console.log('获取用户信息接口数据:', respond)
        commit('SET_ID', respond.data.id)
        commit('SET_NICKNAME', respond.data.nickname)
        commit('SET_AVATAR', respond.data.avatar)
        commit('SET_SEX', respond.data.sex)
        commit('SET_COIN', respond.data.coin)
        commit('SET_VIPLEVEL', respond.data.vipLevel)
        commit('SET_FUNNUM', respond.data.funNum)
        commit('SET_FOLLOWNUM', respond.data.followNum)
        commit('SET_BARNUM', respond.data.barNum)
        commit('SET_EXP', respond.data.exp)
        resolve(respond.data)
      }).catch(error => {
        reject(new Error(error))
      })
    })
  },
  // 修改用户硬币
  updateCoin ({ commit }, num) {
    // 调用修改硬币接口
    updateCoin({ num, id: state.id, token: getToken() }).then(respond => {
      console.log('修改硬币接口返回数据', respond)
      commit('SET_COIN', state.coin + num)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
